#!/bin/sh

trap "rm /tmp/portal.lock" EXIT                                                 

# Check for lockfile
if [ -f /tmp/portal.lock ]
 then
  otherpid=`cat /tmp/portal.lock`
  while [ -e /proc/${otherpid} ]
   do
    sleep 1
   done
  rm -f /tmp/portal.lock
fi

echo $$> /tmp/portal.lock 

usedkey=`grep "pubkey auth succeeded for 'close' with key md5" /var/log/messages | cut -c 102-148`
echo "" > /var/log/messages
usernameb=`grep  $usedkey /mmc/transfer/users.list | cut -d " " -f 3`
givenname=`echo $usernameb | cut -d "|" -f 1`
surname=`echo $usernameb | cut -d "|" -f 2`
rightnow=`date`

echo -ne "\n\nSDC 1\n" >/dev/tts/0                                   

echo "CLOSE $rightnow $givenname $surname" >> /mmc/access.log

exit
